import { MessageBox } from 'mint-ui';


let mutations = {
    // 详情数据的
    changeDetailData(state, data) {
        state.detailData = data
    },
    // 是否显示loading
    showLoading(state, data) {
        state.isLoading = data
    },
    // 分类页面的左侧数据
    categoryLeftDatas(state, data) {
        state.categoryLeftDatas = data
    },
    // 分类页面的右侧数据
    categoryRightDatas(state, data) {
        state.categoryRightDatas = data
    },
    // 商品列表页面
    goodList(state, data) {
        state.goodList = data
    },
    //添加购物车
    //mutations 的载荷只能接受两个参数
    addCarListData(state, data) {

        // 判断店铺是否存在
        // find方法返回第一个满足条件的元素
        let isShop = state.carListData.find((item) => {
            return item.ru_id == data.ru_id
        })
        console.log('isShop', isShop);
        // 判断商品是否存在
        let isGoods = state.carListData.find((item) => {
            return item.children.find((value) => {
                return value.goods_id == data.children[0].goods_id
            })
        })
        console.log('isGoods', isGoods);
        if (isShop) {
            if (isGoods) {
                // 商品存在
                for (let i = 0; i < isGoods.children.length; i++) {
                    if(isGoods.children[i].goods_id == data.children[0].goods_id){
                        isGoods.children[i].buyNum += data.children[0].buyNum*1
                    }
                    
                    isGoods.children[i].buyNum += data.children[0].buyNum*1
                }
                //商品不存在
            }else{
                isShop.children.push(data.children[0])
            }
            //店铺不存在(店铺 购物的东西全部push进去)
        }else{
            state.carListData.push(data)
        }
        localStorage.setItem('carData', JSON.stringify(state.carListData))

    },
    //点击++
    addCarNum(state,data){
        for (let i= 0 ; i<state.carListData.length; i++){
            if(state.carListData[i].ru_id==data.carlist.ru_id){
                state.carListData[i].children[data.index] = data.carlist.children[data.index]
            }
        }
        //最后更新缓存
        localStorage.setItem('carData',JSON.stringify(state.carListData))
    },
        // 选择商品的选择图标
        goodSelect(state,data){
            for(let i=0;i<state.carListData.length;i++){
                if(state.carListData[i].ru_id==data.item.ru_id){//先判断店铺
                    state.carListData[i].children[data.index] = data.item
                    // flag为true代表的该店铺下所有的商品被选中否则有商品没被选中
                    let flag = state.carListData[i].children.every(item=>{
                       return item.isSelect==true
                    })
                    if(flag){
                        state.carListData[i].isShopSelect = true
                    }else{
                        state.carListData[i].isShopSelect = false
                    }
                }
            }
            localStorage.setItem('carData',JSON.stringify(state.carListData))
            
        },
         // 选择店铺前的图标
    shopSelect(state,data){
        for(let i=0;i<state.carListData.length;i++){
            if(state.carListData[i].ru_id==data.item.ru_id){//先判断店铺
                state.carListData[i] = data.item
                state.carListData[i].children.map(item=>{
                    item.isSelect = state.carListData[i].isShopSelect
                })
            }
        }
        state.isSelectAll = state.carListData.every(item=>{
            return item.isShopSelect = true
        })
        localStorage.setItem('carData',JSON.stringify(state.carListData))
        localStorage.setItem('allSelect',JSON.stringify(state.isSelectAll))
    },
    //全选的方法
    selectAll(state){
        state.isSelectAll = ! state.isSelectAll
        state.carListData.map(item=>{
            item.isShopSelect = state.isSelectAll
            item.children.map(item1=>{
                item1.isSelect = state.isSelectAll
            })
        })
        localStorage.setItem('carData',JSON.stringify(state.carListData))
        localStorage.setItem('allSelect',JSON.stringify(state.isSelectAll))
    },
    //删除的操作
   deleteFun(state,data){
    for(let i=0;i<state.carListData.length;i++){
        if(state.carListData[i].ru_id==data.data1.ru_id){
            MessageBox.confirm('确定要删除此宝贝嘛?').then(() => {
                console.log('点击确定按钮了')
                state.carListData[i].children.splice(data.index,1)
                if(state.carListData[i].children.length==0){
                    state.carListData.splice(i,1)
                }
                localStorage.setItem('carData',JSON.stringify(state.carListData))
                
              },()=>{
                console.log('点击取消')
              });
        }
    }
   },



}
export default mutations


